package uebung9.pme;

public class PoorMansExcel {
	private int fieldLength;
	private Entry[] table;
/**
 * first Constructor of the class PoorMansExcel
 * gets the length of rows and creates an "empty"  field ( array) with than length 
 * @param n
 * the length of the table
 */
	public PoorMansExcel(int n) {
		fieldLength = n;
		Entry[] fields = new Entry[n];
		table = fields;
	}
/**
 * second Constructor of this class
 * gets the table and stores its length aswell
 * @param tab
 * the array of Entries
 */
	public PoorMansExcel(Entry[] tab) {
		table = tab;
		fieldLength = table.length;
	}
/**
 * returns the Size of the constructed table (array)
 * @return
 * length of the table
 */
	public int getSize() {
		return fieldLength;
	}
/**
 * changes the length of the table, if the given parameter is less or equal zero the table's length wont change,
 * otherwise the table will lose Entries (if the given number is smaller than the original length)
 * or nulls will be added 
 * @param newLength
 * select here the length of the changed table if less or equal zero is given, don't change the size.
 */
	public void resize(int newLength) {
		if (newLength > 0) {
			fieldLength = newLength;
			Entry[] fields = table; // Backup von table
			table = new Entry[fieldLength]; // Loeschen von table und neues
			// leeres Array
			for (int i = 0; i < fields.length && i < table.length; i++) {
				table[i] = fields[i];
			}
		}
	}
/**
 * change an Entry in a given row. If the change was sucessfull, e.g. the row existed in the table
 * return true, otherwise return false
 * @param row
 * the number of line /Entry which shall be replaced
 * @param subst
 * the Entry wich will replace the old one
 * @return
 */
	public boolean update(int row, Entry subst) {
		if (table.length >= row) {
			table[row - 1] = subst;
			return true;
		} else {
			return false;
		}

	}
/**
 * Creates a String for the table calculation. it will show every entry with its evaluation
 */
	public String toString() {
		String voidstring = new String(
				"                                                            ");
		// das sind meine 60 Leerzeichen MUHAHA XD
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < fieldLength; i++) {
			String usedDescription = new String();
			if (table[i].getDescription().length() < 61) {
				usedDescription = table[i].getDescription();
			} else {
				usedDescription = table[i].getDescription().substring(0, 61);
			}

			if (i < 9) {
				sb = sb.append("  ").append(i++).append(" ").append(
						usedDescription).append(
						voidstring.substring(usedDescription.length())).append(
						table[i].getValue().evaluate()).append('\n');
			} else {
				if (i < 99)// ausserdem soll \n iwie bewirken dass eine neue
				// zeile angefangen wird ka ob es das auch macht xD
				{
					sb = sb.append(" ").append(i++).append(" ").append(
							usedDescription).append(
							voidstring.substring(usedDescription.length()))
							.append(table[i].getValue().evaluate())
							.append('\n');
				} else {
					if (i < 999) {
						sb = sb.append(i++).append(i++).append(" ").append(
								usedDescription).append(
								voidstring.substring(usedDescription.length()))
								.append(table[i].getValue().evaluate()).append(
										'\n');
					}
				}
			}
		}
		return sb.toString();
	}
}